package qy.course.cse494.view;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.border.BevelBorder;

import qy.course.cse494.ir.DocumentHits;
import qy.course.cse494.ir.DocumentIndex;
import qy.course.cse494.ir.DocumentQuery;
import qy.course.cse494.ir.DocumentSearch;
import qy.course.cse494.web.WebAHRanker;
import qy.course.cse494.web.WebLinksExtractor;
import qy.course.cse494.web.WebPageRanker;

public class SearchEngineEntryFrame extends JFrame
{

	/**
	 * 
	 */
	private static final long serialVersionUID = -2375272371280463559L;
	private JTextField pleaseEnterTheTextField;
	
	private int _method_choice = 0;

	/////////////////////////////////////////////////////////////////
	DocumentIndex di = new DocumentIndex("data/index4result3", true);
	DocumentSearch searcher = new DocumentSearch(di);
	WebLinksExtractor web_links_extractor = new WebLinksExtractor("data/Hashedlinks4");
	/////////////////////////////////////////////////////////////////
	/**
	 * Launch the application
	 * @param args
	 */
	public static void main(String args[])
	{
		try
		{
			SearchEngineEntryFrame frame = new SearchEngineEntryFrame();
			frame.setVisible(true);
		} catch(Exception e)
		{
			e.printStackTrace();
		}
	}

	/**
	 * Create the frame
	 */
	public SearchEngineEntryFrame()
	{
		super();
		setTitle("Search Engine");
		setBounds(100, 100, 500, 375);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		final JPanel panel = new JPanel();
		panel.setLayout(new FlowLayout());
		panel.setBorder(new BevelBorder(BevelBorder.LOWERED));
		getContentPane().add(panel, BorderLayout.SOUTH);

		final JScrollPane scrollPane = new JScrollPane();
		getContentPane().add(scrollPane);

		final JTextPane textPane = new JTextPane();
		scrollPane.setViewportView(textPane);
		textPane.setEditable(false);
		
		pleaseEnterTheTextField = new JTextField();
		panel.add(pleaseEnterTheTextField);
		pleaseEnterTheTextField.setText("Please enter the query here");

		final JButton searchButton = new JButton();
		panel.add(searchButton);
		searchButton.setName("button");
		searchButton.setText("Search ...");
		searchButton.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent e)
			{
				System.out.println(pleaseEnterTheTextField.getText());
				//
				String query_str = pleaseEnterTheTextField.getText();
				DocumentQuery query = new DocumentQuery(query_str);
				DocumentHits hits = null;
				String output = "";
				switch(_method_choice)
				{
				case 0:
					hits = searcher.search(query, 10);
					WebPageRanker wpr = new WebPageRanker(web_links_extractor);
					wpr.compute();
					hits = wpr.search(hits, di);
					//
					output = "Weight : page-rank :: " + wpr.get_rel_impotance_weight()
						+ " vectorspace-similairy :: " + (1-wpr.get_rel_impotance_weight())+ "\n";
					break;
				case 1:
				case 2:
					hits = searcher.search(query, 10);
					int size_of_seed_set = hits.length();
			    	Vector<Integer> seed_list = new Vector<Integer>();
			    	for(int i=0; i<hits.length(); ++i)
			    	{
			    		String file_name = di.get_doc_name(hits.get_doc_id_ranked_at(i));
			    		seed_list.add(file_name.hashCode());
			    	}
					WebAHRanker ahranker = new WebAHRanker(web_links_extractor);
					ahranker.search(seed_list, 10);
					if(_method_choice == 1)
					{
						hits = ahranker.get_top_authority_hits();
					}else
					{
						hits = ahranker.get_top_hub_hits();
					}
					//
					output = "Pages before extension : " + size_of_seed_set + "\n";
					output += "Page after extension : " + ahranker.get_ah_list().size()+ "\n";
					break;
				case 3:
					hits = searcher.search(query, 100);
				}
				//
				if(hits == null) return;
				int hit_num = hits.length();
				
				for(int i=0; i<hit_num; ++i)
				{
					output += i + " : "+hits.get_weight_ranked_at(i)+" : ";
	    					//+ hits.get_doc_id_ranked_at(i) + " :: ";
					switch(_method_choice)
					{
					case 3:
						output += di.get_doc_name(hits.get_doc_id_ranked_at(i)) + "\n";
						break;
					case 2:						
					case 1:
					case 0:
						output += di.get_doc_name_from_hashcode(hits.get_doc_id_ranked_at(i)) + "\n";
					}
				}
				textPane.setText(output);
			}
		});

		final JMenuBar menuBar = new JMenuBar();
		setJMenuBar(menuBar);

		final JMenu fileMenu = new JMenu();
		fileMenu.setText("File");
		menuBar.add(fileMenu);

		final JMenuItem exitMenuItem = new JMenuItem();
		exitMenuItem.setText("Exit");
		fileMenu.add(exitMenuItem);

		final JMenu methodMenu = new JMenu();
		methodMenu.setText("Method");
		menuBar.add(methodMenu);

		ButtonGroup method_menu_group = new ButtonGroup();
		
		final JRadioButtonMenuItem pageRankRadioButtonMenuItem = new JRadioButtonMenuItem();
		pageRankRadioButtonMenuItem.setText("Page Rank");
		method_menu_group.add(pageRankRadioButtonMenuItem);
		methodMenu.add(pageRankRadioButtonMenuItem);
		pageRankRadioButtonMenuItem.addActionListener(new ActionListener(){

			public void actionPerformed(ActionEvent arg0)
			{
				System.out.println("page rank is selected!");
				_method_choice = 0;
			}

		});

		final JRadioButtonMenuItem authorityRadioButtonMenuItem = new JRadioButtonMenuItem();
		authorityRadioButtonMenuItem.setText("Authority");
		method_menu_group.add(authorityRadioButtonMenuItem);
		methodMenu.add(authorityRadioButtonMenuItem);
		authorityRadioButtonMenuItem.addActionListener(new ActionListener(){

			public void actionPerformed(ActionEvent arg0)
			{
				System.out.println("authority is selected!");
				_method_choice = 1;
			}

		});
		
		final JRadioButtonMenuItem hubRadioButtonMenuItem = new JRadioButtonMenuItem();
		hubRadioButtonMenuItem.setText("Hub");
		method_menu_group.add(hubRadioButtonMenuItem);
		methodMenu.add(hubRadioButtonMenuItem);
		hubRadioButtonMenuItem.addActionListener(new ActionListener(){

			public void actionPerformed(ActionEvent arg0)
			{
				System.out.println("hub is selected!");
				_method_choice = 2;
			}

		});

		final JRadioButtonMenuItem tfidfRadioButtonMenuItem = new JRadioButtonMenuItem();
		tfidfRadioButtonMenuItem.setText("TF-IDF");
		method_menu_group.add(tfidfRadioButtonMenuItem);
		methodMenu.add(tfidfRadioButtonMenuItem);
		tfidfRadioButtonMenuItem.addActionListener(new ActionListener(){

			public void actionPerformed(ActionEvent arg0)
			{
				System.out.println("tf-idf is selected!");
				_method_choice = 3;
			}

		});
		
		
		final JMenu helpMenu = new JMenu();
		helpMenu.setText("Help");
		menuBar.add(helpMenu);

		final JMenuItem aboutMenuItem = new JMenuItem();
		aboutMenuItem.setText("About");
		helpMenu.add(aboutMenuItem);
		//
	}

}
